package com.iman.Confucius.service.impl.student;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.iman.Confucius.domain.MessageDetailBean;
import com.iman.Confucius.service.MongoMessageDetailService;
import com.iman.Confucius.service.student.CaseService;
import com.iman.Confucius.vo.CaseVo;
import com.iman.common.hibernate.TemplateQLService;
import com.iman.common.support.Page;

@Service
public class CaseServiceImpl implements CaseService{

	@Inject
	TemplateQLService templateQLService;
	@Inject
	MongoMessageDetailService mongoService;
	
	@Override
	public Page<CaseVo> getCasePage(Map<String, Object> params) {
		Integer ps = Integer.valueOf(params.get("ps").toString());
		Integer pn = Integer.valueOf(params.get("pn").toString());
		params.put("startNo", ps*pn);
 		Page<CaseVo> vos = templateQLService.findByPagedSQLBeanQuery(
				"case_page_list", params, Page.getStartOfPage(pn, ps), ps,
				CaseVo.class);
		return vos;
	}

	@Override
	public int addCase(Map<String, Object> params) {
		//case_handle_content  数据内容存储到mongodb		
		String case_handle_content = params.get("case_handle_content").toString();
		MessageDetailBean beanContent = new MessageDetailBean();
		beanContent.setDetailContent(case_handle_content);
		mongoService.save(beanContent);
		params.put("case_handle_content", beanContent.getId());
		

		//case_cure_result  数据内容存储到mongodb		
		String case_cure_result = params.get("case_cure_result").toString();
		MessageDetailBean beanResult = new MessageDetailBean();
		beanResult.setDetailContent(case_cure_result);
		mongoService.save(beanResult);
		params.put("case_cure_result", beanResult.getId());
		
		templateQLService.executeSQLUpdate("case_add", params);

		return 0;
	}

	@Override
	public int editCase(Map<String, Object> params) {
		
		//case_handle_content  数据内容存储到mongodb		
		MessageDetailBean beanContent = new MessageDetailBean();
		beanContent.setId(params.get("case_handle_content_id").toString());
		beanContent.setDetailContent( params.get("case_handle_content").toString());
		mongoService.save(beanContent);
		params.put("case_handle_content", beanContent.getId());
		
		//case_cure_result  数据内容存储到mongodb		
		MessageDetailBean beanResult = new MessageDetailBean();
		beanResult.setId(params.get("case_cure_result_id").toString());
		beanResult.setDetailContent(params.get("case_cure_result").toString());
		mongoService.save(beanResult);
		params.put("case_cure_result", beanResult.getId());

		return templateQLService.executeSQLUpdate("case_edit", params); 
	}


	@Override
	public int delCase(Map<String, Object> params) {
		return  templateQLService.executeSQLUpdate("case_del", params); 
	}

	@Override
	public CaseVo getDetail(Map<String, Object> params) {
	 List<CaseVo>  cases=	 templateQLService.findBySQLBeanQuery("case_detail",	params, CaseVo.class);
	 if(cases != null && cases.size()>0)
	 {
		 CaseVo casevo =  cases.get(0);	 
		 
		 //从mongodb中获取case_cure_result  的值		
		MessageDetailBean beanContent=	mongoService.findById(casevo.getCase_handle_content());
		casevo.setCase_handle_content( beanContent.getDetailContent());
		casevo.setCase_handle_content_id(beanContent.getId());
		
		//从mongodb中获取case_cure_result  数据内容存储到mongodb 
		MessageDetailBean beanResult = mongoService.findById(casevo.getCase_cure_result());
 		casevo.setCase_cure_result( beanResult.getDetailContent());
 		casevo.setCase_cure_result_id(beanResult.getId());
		return casevo;
	 }
	 return null;
 		
	}
	
 	
}
